from PySide6.QtWidgets import QApplication
from LoginWindow import *
from DBAccessor import *
from MainWindow import *
from AccountManager import *


if __name__ == '__main__':
    # 初始化数据库
    print("Initializing database...")
    dbaccessor = DBAccessor()  # 初始化数据库
    # try:
    #     dbaccessor = DBAccessor()  # 初始化数据库
    # except Exception as e:
    #     print("Error occurred when initializing database...\n", e)
    #     exit(1)
    print("Database initialized successfully.")

    # 构造账户管理器
    account_manager = AccountManager()

    app = QApplication([])
    # 构造登入窗口，传入屏幕分辨率
    login_window = Login_Dialog(account_manager, app.primaryScreen().size().width(), app.primaryScreen().size().height(), dbaccessor)
    login_window.show()
    app.exec()
    if account_manager.uid == -1:
        exit(0)

    # 创建视图
    dbaccessor.init_view(account_manager.uid)

    # 主窗口，在登录完成后启动
    main_window = Main_Window(account_manager, app.primaryScreen().size().width(), app.primaryScreen().size().height(), dbaccessor)
    main_window.show()
    app.exec()
